//
//  ST_SameCityCell.swift
//  btvideo
//
//  Created by mac on 2019/12/10.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SwiftyJSON
import HandyJSON

class ST_SameCityCell: UICollectionViewCell {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets.zero)
        }
        
        self.contentView.addSubview(maskLayer)
        maskLayer.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(100)
        }
        
        contentView.addSubview(user_info_describe_label)
        user_info_describe_label.snp.makeConstraints { (make) in
            make.left.equalTo(contentView).offset(3)
            make.bottom.equalTo(contentView).offset(-10)
        }
        
        contentView.addSubview(nickname_label)
        nickname_label.snp.makeConstraints { (make) in
            make.left.equalTo(contentView).offset(3)
            make.bottom.equalTo(user_info_describe_label.snp.top).offset(-5)
        }
        
        contentView.addSubview(love_btn)
        love_btn.snp.makeConstraints { (make) in
            make.right.equalTo(contentView).offset(-10)
            make.bottom.equalTo(contentView).offset(-10)
        }
        
        
        contentView.backgroundColor = Color.clear
        
        contentView.addSubview(mask_effect_view)
        mask_effect_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.cornerRadius = 8
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    lazy var imageView:UIImageView = {
       let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        return control
    }()
    
    lazy var nickname_label:UILabel = {
       let control = UILabel()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        
        return control
    }()
    
    
    lazy var user_info_describe_label:UILabel = {
        let control = UILabel()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    lazy var love_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setImageForAllStates(ST_ImageUtils.imageName("消息_icon_超级喜欢")!)
        
//        control.setImage(ST_ImageUtils.imageName("消息_icon_超级喜欢"), for: UIControl.State.selected)
        
        control.addTarget(self, action: #selector(love_handle), for: UIControl.Event.touchUpInside)
        return control
    }()

    
    
    // fillCode
    lazy var maskLayer:ST_FWGradientView = {
        let control = ST_FWGradientView()
        control.bgLayer.colors = [UIColor.init(white: 1, alpha: 0).cgColor,UIColor.init(white: 0, alpha: 0.8).cgColor]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
        return control
    }()
    
    fileprivate lazy var mask_effect_view:UIVisualEffectView = {
            
        let blur = UIBlurEffect.init(style: UIBlurEffect.Style.regular)
        
        let control = UIVisualEffectView.init(effect: blur)
        
        
        let vibrancy =  UIVibrancyEffect.init(blurEffect: blur)
        
        let vibrancyEffectView = UIVisualEffectView.init(effect: vibrancy)
        
        //注意是 blurEffectView.contentView. 苹果官方注释说了  Do not add subviews directly to UIVisualEffectView, use this view instead
        
        control.contentView.addSubview(vibrancyEffectView)
        
        vibrancyEffectView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
            
        control.alpha = 0
        
        return control
    }()
    
    
    var model = ST_SameCityCellModel(){
        didSet{
            
            imageView.pin_setImage(from: model.cover_img.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            nickname_label.text = model.nickname
       
            user_info_describe_label.text = model.distance
            
            love_btn.isSelected = model.loved
            
        }
    }
    
    
    var display_mask_effect = false{
        didSet{
            
            mask_effect_view.alpha = display_mask_effect ? 1 : 0
            
            maskLayer.isHidden = display_mask_effect
        }
    }
    
//    MARK: - Event
    var love_closure:((ST_SameCityCellModel)->())?
    @objc func love_handle(){
        
        
        
        if self.love_btn.isSelected == false{
            self.love_closure?(model)
        }
        
        
    }
}

class ST_SameCityCellModel: NSObject,HandyJSON {
    var cover_img = ""
    var nickname = ""
    var like_rate = ""
    var distance = ""
    var loved = false
    var accid = ""
    var avatar = ""
    
    required override init() {
        super.init()
    }
}
